﻿const CopyPlugin = require("copy-webpack-plugin");
const ExampleBuilder = require("./example-builder");
const fs = require("fs");
const path = require("path");

const src = path.join(__dirname, "..");

//筛选所有html文件
const examples = fs
  .readdirSync(src)
  .filter((name) => /^(?!index|editor).*\.html$/.test(name))
  .map((name) => name.replace(/\.html$/, ""));

//构建包含所有js的对象
const htmls = {};
examples.forEach((example) => {
  htmls[example] = `${example}.html`;
});

module.exports = {
  context: src,
  target: "web",
  entry: "./index.js",
  module: {
    rules: [
      {
        //use: {
        //    loader: 'buble-loader'
        //},
        test: /\.js/,
        include: [
          path.join(__dirname, "..", "..", "src"),
          path.join(__dirname, ".."),
        ],
      },
    ],
  },
  plugins: [
    //创建文件
    new ExampleBuilder({
      templates: path.join(__dirname, "..", "template"),
      htmls: htmls,
      // common: 'common'
    }),
    new CopyPlugin([
      { from: "js", to: "js" },
      { from: "css", to: "css" },
      { from: "template", to: "template" },
      { from: "resources", to: "resources" },
      { from: "index.html", to: "index.html" },
      { from: "editor.html", to: "editor.html" },
    ]),
  ],
  devtool: "eval",
  output: {
    filename: "index.js",
    path: path.join(__dirname, "..", "..", "build", "examples"),
  },
};
